/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server.data;

import java.text.ParseException;
import server.AuctionManager;

/**
 * Represents a Group Bid
 * @author Group 95
 */
public class Confirmation {
    private final long auctionId;
    private final double dPrice;
    private final String strBidder;
    private final User confirmer;
    
    final public static String USAGE="Invalid parameters, Usage: !confirm <auction-id> <amount> <biddername>!";
    final public static String INV_PARAMETERS="Invalid parameters, the first two have to be numeric!";
    
    /**
     * Bid Constructor
     * @param strParts String [] strLine[0]="!confirm", strLine[1]=<auction_id>, strLine[2]=<amount>, strLine[3]=<biddername>
     * @param bidder User that did the bid
     */
    public Confirmation(final String [] strParts,final User confirmer) throws InvalidCommandException
    {
        try 
        {
            this.auctionId = Long.parseLong(strParts[1]);
            this.dPrice = AuctionManager.getInstance().parseDouble(strParts[2]);
            this.strBidder = strParts[3].toLowerCase();
            this.confirmer=confirmer;
        }
        catch (IndexOutOfBoundsException ex) 
        {
            throw new InvalidCommandException(USAGE,ex);
        }
        catch (NumberFormatException ex) 
        {
            throw new InvalidCommandException(INV_PARAMETERS,ex);
        }
        catch (ParseException ex) 
        {
            throw new InvalidCommandException(INV_PARAMETERS,ex);
        }
    }

    public long getAuctionId() {
        return auctionId;
    }
    
    public String toAuctionString()
    {
        StringBuilder strB=new StringBuilder(Long.toString(this.auctionId)).append(' ');
        strB.append(AuctionManager.getInstance().formatDouble(this.dPrice)).append(' ');
        strB.append(this.strBidder);
        return strB.toString();
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Confirmation other = (Confirmation) obj;
        if (this.auctionId != other.auctionId) {
            return false;
        }
        if (Double.doubleToLongBits(this.dPrice) != Double.doubleToLongBits(other.dPrice)) {
            return false;
        }
        if ((this.strBidder == null) ? (other.strBidder != null) : !this.strBidder.equals(other.strBidder)) {
            return false;
        }
        if (this.confirmer != other.confirmer && (this.confirmer == null || !this.confirmer.equals(other.confirmer))) {
            return false;
        }
        return true;
    }
    
    public User getConfirmer()
    {
        return this.confirmer;
    }
}
